//
//  LTHeaderView.m
//
//
//  Created by admin on 17/6/20.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "LTHeaderView.h"
#import "LCVideoHomeListAboutModel.h"
#import "UIViewController+LCMobShare.h"

@interface LTHeaderView ()

@property(strong, nonatomic) UILabel *titleLabel;
@property(strong, nonatomic) LCTvOSCardView *tvOSCardView;
@property(strong, nonatomic) UILabel *authorLabel;
@property(strong, nonatomic) UILabel *zanLabel;
@property(assign, nonatomic) BOOL isZan;
@property(strong, nonatomic) UILabel *browseLabel;//浏览次
@property(strong, nonatomic) UILabel *shareLabel;
@property(strong, nonatomic) UILabel *videoInfoAboutLabel;//视频全部资料

@end

@implementation LTHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}
- (void)dealloc {
    NSLog(@"__func__dealloc = %s", __func__);
}
-(void)setMp4DetailModel:(LCMp4DetailModel *)mp4DetailModel{
    _mp4DetailModel = mp4DetailModel;
    if (_mp4DetailModel) {
//        [self initClickZanAll];
//        [_horizontalGrideView reloadData];
//\t赞:%@,self.mp4DetailModel.detail.zan
        _videoInfoAboutLabel.text =
        [NSString stringWithFormat:@"简介:%@",_mp4DetailModel.detail.content];
        self.browseLabel.text = [NSString stringWithFormat:@"浏览:%@次",self.mp4DetailModel.detail.click];
    }
}
#pragma mark - 布局子视图
- (void)setupSubviews {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.titleLabel];
    [self addSubview:self.tvOSCardView];
    [self addSubview:self.authorLabel];

    [self.tvOSCardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.top.equalTo(self).offset(20);
        if (ScreenWidth == 320) {
            make.width.mas_equalTo(140*0.5);
        }else{
            make.width.mas_equalTo(150*0.5);
        }
        make.height.equalTo(self.tvOSCardView.mas_width).multipliedBy(1.44);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tvOSCardView.mas_right).offset(30);
        make.top.equalTo(self.tvOSCardView.mas_top).offset(0);
        make.right.equalTo(self).offset(-30);
    }];
    [self.authorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tvOSCardView.mas_right).offset(30);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(20);
        make.right.equalTo(self).offset(-30);
    }];
    [self.browseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.authorLabel.mas_left);
        make.top.equalTo(self.authorLabel.mas_bottom).offset(20);
        make.right.equalTo(self).offset(-30);
    }];
    [self.videoInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.videoInfoLabel);
        make.bottom.equalTo(self.videoInfoAboutLabel.mas_top).offset(-5);
        make.right.equalTo(self).offset(-20);
    }];
    [self.videoInfoAboutLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tvOSCardView.mas_left);
        make.top.equalTo(self.tvOSCardView.mas_bottom).offset(20);
        make.right.equalTo(self).offset(-20);
        make.height.mas_equalTo(60);
    }];
    [self.commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.bottom.equalTo(self.mas_bottom).offset(-20);
        make.right.equalTo(self).offset(-20);
        make.height.mas_equalTo(30);
    }];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = RGBColor(243, 243, 243);//huibai色
    [self insertSubview:bgView atIndex:0];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(15);
    }];
    UIView *bgWhiteView = [[UIView alloc] init];
    [bgWhiteView addShadow];
    bgWhiteView.layer.masksToBounds = NO;
//    //设置阴影的高度
//    bgWhiteView.layer.shadowOffset = CGSizeMake(0, 0.5);
//    //设置透明度
//    bgWhiteView.layer.shadowOpacity = 0.2;
//    bgWhiteView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    // 设置圆角大小
    bgWhiteView.layer.cornerRadius = 11;

    bgWhiteView.backgroundColor = [UIColor whiteColor];//bai色
    [self insertSubview:bgWhiteView atIndex:1];
    [bgWhiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.right.equalTo(self);
        make.bottom.equalTo(bgView.mas_top).offset(10);
    }];
    __weak typeof(self) weakSelf = self;
    [bgWhiteView tapGesture:^(UIGestureRecognizer *ges) {
        [weakSelf endEditing:YES];
    }];
    
}
- (void)setListAboutModel:(LCVideoHomeListAboutModel *)listAboutModel {
    _listAboutModel = listAboutModel;
    NSURL *url = [NSURL URLWithString:listAboutModel.pic];
    [_tvOSCardView.cardImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"视频大的默认图片"]];
    _titleLabel.text = listAboutModel.name;
    _authorLabel.text = [NSString stringWithFormat:@"作者:%@",listAboutModel.author];
}
- (LCTvOSCardView *)tvOSCardView {
    if (!_tvOSCardView) {
        _tvOSCardView = [[LCTvOSCardView alloc] init];
        _tvOSCardView.center = self.center;
        _tvOSCardView.bounds = CGRectMake(0, 0, 150*0.5, 150*0.5*1.44);
        if (ScreenWidth == 320) {
            _tvOSCardView.bounds = CGRectMake(0, 0, 140*0.5, 140*0.5*1.44);
        }
    }
    return _tvOSCardView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
//        _titleLabel.text = @"title";
        _titleLabel.textColor = titleColorLSZ;
        _titleLabel.font = [LSZSpecialPackaging setlabelTextFontAdaptiveSize:15];
        _titleLabel.numberOfLines = 0;
//        _titleLabel.userInteractionEnabled = YES;
//        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tagGesture:)];
//        [_titleLabel addGestureRecognizer:gesture];
    }
    return _titleLabel;
}

- (UILabel *)authorLabel {
    if (!_authorLabel) {
        UILabel *labelAuthor = [[UILabel alloc]init];
        _authorLabel = labelAuthor;
        labelAuthor.textColor = RGBColor(103,137,177);
        labelAuthor.font = [LSZSpecialPackaging setlabelTextFontAdaptiveSize:14];;
        labelAuthor.numberOfLines = 0;
    }
    return _authorLabel;
}
- (UILabel *)browseLabel {
    if (!_browseLabel) {
        _browseLabel = [[UILabel alloc]init];
        _browseLabel.textColor = RGBColor(103,137,177);
        _browseLabel.font = [LSZSpecialPackaging setlabelTextFontAdaptiveSize:14];;
        _browseLabel.numberOfLines = 0;
        [self addSubview:_browseLabel];
    }
    return _browseLabel;
}

- (UILabel *)videoInfoLabel {
    if (!_videoInfoLabel) {
        _videoInfoLabel = [[UILabel alloc]init];
        _videoInfoLabel.textColor = redUIColor;
        _videoInfoLabel.font = [LSZSpecialPackaging setlabelTextFontAdaptiveSize:14];;
        _videoInfoLabel.numberOfLines = 0;
        _videoInfoLabel.text = @"全部资料";
        [self addSubview:_videoInfoLabel];
    }
    return _videoInfoLabel;
}
- (UILabel *)videoInfoAboutLabel {
    if (!_videoInfoAboutLabel) {
        _videoInfoAboutLabel = [[UILabel alloc]init];
        _videoInfoAboutLabel.textColor = lightColor;
        _videoInfoAboutLabel.font = [LSZSpecialPackaging setlabelTextFontAdaptiveSize:14];;
        _videoInfoAboutLabel.numberOfLines = 3;
        [self addSubview:_videoInfoAboutLabel];
    }
    return _videoInfoAboutLabel;
}
-(UIButton *)commentButton{
    if (!_commentButton) {
        _commentButton = [[UIButton alloc]init];
        [_commentButton setTitleColor:redUIColor forState:(UIControlStateNormal)];
        [_commentButton setImage:[UIImage imageNamed:@"点评一下"] forState:UIControlStateNormal];
        [_commentButton setTitle:@"  点评一下" forState:(UIControlStateNormal)];
        _commentButton.titleLabel.font = [LSZSpecialPackaging setlabelTextFontAdaptiveSize:13];
        [_commentButton.layer setCornerRadius:7.0]; //设置矩形四个圆角半径
        [_commentButton.layer setBorderWidth:0.50];
        [_commentButton.layer setBorderColor:redUIColor.CGColor];//边框颜色
        _commentButton.clipsToBounds = YES;
        @weakify(self)
        [_commentButton tapGesture:^(UIGestureRecognizer *ges) {
            @strongify(self)
            [self commentButtonClick];
        }];
        [self addSubview:_commentButton];
    }
    return _commentButton;
}
//- (void)tagGesture:(UITapGestureRecognizer *)gesture {
//    NSLog(@"响应事件，回调自己处理吧。");
//}
- (void)commentButtonClick
{
    @weakify(self)
    ACActionSheet *actionSheet = [[ACActionSheet alloc] initWithTitle:@"" cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"喜欢",@"不喜欢"] actionSheetBlock:^(NSInteger buttonIndex) {
        @strongify(self)
        NSLog(@"ACActionSheet block - %ld",buttonIndex);
        if (buttonIndex == 0) {
            [self clickZan];
        }
        if (buttonIndex == 1) {
            if([[_UserM usercode] isEqualToString:@""]){
                if (self.clickPushLoginBlock != nil) {
                    self.clickPushLoginBlock();
                }
                return;
            }
            [self setStringWarning:@"点评成功!"];
        }
    }];
    [actionSheet show];
}

#pragma mark - 用户对资源点赞    type,资源类型     1,音频 2，视频 3，文章，6祷告，7见证
-(void)clickZan {
    if([[_UserM usercode] isEqualToString:@""]){
        if (_clickPushLoginBlock != nil) {
            _clickPushLoginBlock();
        }
        return;
    }
    if (self.isZan == YES) {
        [self setStringWarning:@"不能重复点评!"];
        return;
    }
    __weak typeof(self) weakSelf = self;
    [LSZRequestData UserVersusResourceThumbUp:self.listAboutModel.id type:@"2" :^(BOOL isOk, NSString * str, NSDictionary * dic) {
        if (isOk == YES) {
            weakSelf.isZan = YES;
            [weakSelf setStringWarning:@"点评成功!"];
        }else{
            [weakSelf setStringWarning:str];
        }
    }];
}

#pragma mark - hub的弹窗
- (void)setStringWarning:(NSString *)str {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
    hud.mode = MBProgressHUDModeText;//枚举类型不同的效果
    hud.label.text= [NSString stringWithFormat:@"%@",str];
    [self performSelector:@selector(hiddenMBAction) withObject:nil afterDelay:1.0];
}
- (void)hiddenMBAction {
    [MBProgressHUD hideHUDForView:self animated:YES];
}

@end
